# this version of getCSPPattern filters for candlestick patterns without trend

getCSPPattern100 <- function(TS, from="1992", to=NULL) {
  if (!is.xts(TS)) {
    stop("TS argument supplied to getCSPPattern is not a xts object")
  }
  Hammer <- CSPHammer(TS)
  colnames(Hammer) <- "Hammer_A"
  InvHammer <- CSPInvertedHammer(TS)
  colnames(InvHammer) <- "InvertedHammer_A"
  Marubozu <- CSPMarubozu(TS)
  WhiteMarubozu <- Marubozu[,"WhiteMarubozu"]
  colnames(WhiteMarubozu) <- "WhiteMarubozu_A"
  BlackMarubozu <- Marubozu[,"BlackMarubozu"]
  colnames(BlackMarubozu) <- "BlackMarubozu_A"
  Doji <- CSPDoji(TS)
  DojiA <- Doji[,"Doji"]
  colnames(DojiA) <- "Doji_A"
  # calculate ATR for Doji
  ATRlength <- ATR(cbind(Hi(TS), Lo(TS), Cl(TS)), n=20, maType="SMA")[,"atr"]
  LongDFDoji <- Doji[,"DragonflyDoji"] & 
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  colnames(LongDFDoji) <- "DragonflyDoji_A"
  LongGSDoji <- Doji[,"GravestoneDoji"] & 
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  colnames(LongGSDoji) <- "GravestoneDoji_A"

  GAP <- CSPGap(TS)
  GapUp <- GAP[,"GapUp"]
  colnames(GapUp) <- "GapUp_A"
  GapDown <- GAP[,"GapDown"]
  colnames(GapDown) <- "GapDown_A"
  TasukiGap <- CSPTasukiGap(TS)
  UpTG <- TasukiGap[,"UpsideTasukiGap"]
  colnames(UpTG) <- "UpsideTasukiGap_A"
  DownTG <- TasukiGap[,"DownsideTasukiGap"]
  colnames(DownTG) <- "DownsideTasukiGap_A"
  Engulfing <- CSPEngulfing(TS)
  BullEngulfing <- Engulfing[,"Bull.Engulfing"]
  colnames(BullEngulfing) <- "BullEngulfing_A"
  BearEngulfing <- Engulfing[,"Bear.Engulfing"]
  colnames(BearEngulfing) <- "BearEngulfing_A"
  PiercingPattern <- CSPPiercingPattern(TS)
  colnames(PiercingPattern) <- "PiercingPattern_A"
  DarkCloudCover <- CSPDarkCloudCover(TS)
  colnames(DarkCloudCover) <- "DarkCloudCover_A"
  Stomach <- CSPStomach(TS)
  ATStomach <- Stomach[,"AboveTheStomach"]
  colnames(ATStomach) <- "AboveTheStomach_A"
  BTStomach <- Stomach[,"BelowTheStomach"] 
  colnames(BTStomach) <- "BelowTheStomach_A"
  Harami <- CSPHarami(TS)
  BullHarami <- Harami[,"Bull.Harami"]
  colnames(BullHarami) <- "BullHarami_A"
  BearHarami <- Harami[,"Bear.Harami"]
  colnames(BearHarami) <- "BearHarami_A"
  Star <- CSPStar(TS)
  MorningStar <- Star[,"MorningStar"]
  colnames(MorningStar) <- "MorningStar_A"
  EveningStar <- Star[,"EveningStar"]
  colnames(EveningStar) <- "EveningStar_A"
  ThreeWhiteSoldiers <- CSPThreeWhiteSoldiers(TS)
  colnames(ThreeWhiteSoldiers) <- "ThreeWhiteSoldiers_A"
  ThreeBlackCrows <- CSPThreeBlackCrows(TS)
  colnames(ThreeBlackCrows) <- "ThreeBlackCrows_A"

  if (is.null(to)) {
    subsetstring = paste(from, "/", sep="")
  } else {
    subsetstring = paste(from, to, sep="/")
  }
  result <- cbind(
    Hammer,
    InvHammer,
    WhiteMarubozu,
    BlackMarubozu,
    DojiA,
    LongDFDoji,
    LongGSDoji,
    GapUp,
    GapDown,
    UpTG,
    DownTG,
    BullEngulfing,
    BearEngulfing,
    PiercingPattern,
    DarkCloudCover,
    ATStomach,
    BTStomach,
    BullHarami,
    BearHarami,
    MorningStar,
    EveningStar,
    ThreeWhiteSoldiers,
    ThreeBlackCrows
    )
  result$Random_A <- FALSE
  # random 5% of bars
  result$Random_A[sample(1:nrow(result),round(nrow(result)/20),replace=FALSE),]<-TRUE
  return (result[subsetstring])
}
